package tech.yangzhijie.basic.java03;

import java.io.FileNotFoundException;
import java.text.DecimalFormat;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

import static java.time.DayOfWeek.SUNDAY;

public class Java03 {

    static public void main(String[] args) throws FileNotFoundException {
        /*try (Scanner scanner = new Scanner(new File("E:\\dev\\idea\\example\\java-01\\src\\file\\abc.txt"))) {

            while (scanner.hasNextInt()){
                int i = scanner.nextInt();
                System.out.println(i);
            }


        }*/
        /*int i = 10;
        switch (i){
            case 10:
                System.out.println(i);
            case 12:
                System.out.println("12");
                break;
            case 13:
                System.out.println("12");
                System.out.println("12");
            default:
                System.out.println("default");
        }*/

        /*int k =7;
        do{
            System.out.println(k--);
            k--;
        }while (k >0);*/
        /*double money;int years = 0;
        double m = 10000;
        double r = 1;
        double k = 5;
        money = m;
        while (money < m*k){
            years = years + 1;
            money *= (years + r);
        }
        System.out.println(years);
        System.out.println(money);*/
        /*m1(0);
        m1(2);
        m1(4);
        m1(6);
        m1(8);
        m1(10);
        m2(3);
        m3(3);
        m4();
        System.out.println();
        m5();
        System.out.println();
        m6_1(9,9);
        System.out.println();
        m6_2(9,9);
        System.out.println();
        m6_3(9,9);*/

        //System.out.println();
        //m7();
        //System.out.println();
        //m8();
        //m9();
        //fibonacci(100);

        //m10();
        //m11();
        //m12();
        //m13();
        //m14();
        Map<String,String> map = new HashMap<>();
        map.put("1","1");
        map.put("1","2");
        map.forEach((k,v)->{
            System.out.println(k+"-"+v);
        });
    }

    private static void m1(int n) {
        int num = n;
        for (int i = 0; i < num; i++) {
            n *= (i + 1);
            if (num == 0) {
                n = 1;
                break;
            }
        }
        System.out.printf("%s! 的结果为 %s\n", num, n);
    }

    private static void m2(int n) {
        double num = 0.0;
        for (int i = 1; i <= n; i++) {
            num += 1.0 / i;
        }
        System.out.printf("第二题的结果为 %s\n", num);
    }

    private static void m3(int n) {
        double num = 0;
        for (int i = 0; i <= n; i++) {
            if (i == 0) {
                num = 1;
            } else
                num += (1.0 / (3 * i));
        }
        System.out.printf("第三题的结果为 %s\n", num);
    }

    private static void m4() {
        for (int i = 0; i < 4; i++) {
            for (int m = 0; m < i; m++) {
                System.out.print("\t");
            }
            for (int j = 0; j < 2 * (4 - i) - 1; j++) {
                System.out.print("*\t");
            }
            System.out.println();
        }
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3 - i - 1; j++) {
                System.out.print("\t");
            }
            for (int K = 0; K < 2 * i + 3; K++) {
                System.out.print("*\t");
            }
            System.out.println();
        }
    }

    private static void m5() {
        for (int i = 0; i < 10; i++) {
            for (int j = i; j < 10; j++) {
                System.out.print("*\t");
            }
            System.out.println();
        }
    }

    private static void m6_1(int x, int y) {
        for (int i = 1; i <= x; i++) {
            for (int j = i; j <= y; j++) {
                System.out.printf("%s * %s = %s\t", i, j, i * j);
            }
            System.out.println();
        }
    }

    private static void m6_2(int x, int y) {
        for (int i = x; i > 0; i--) {
            for (int j = y; j >= i; j--) {
                System.out.printf("%s * %s = %s\t", j, i, i * j);
            }
            System.out.println();
        }
    }

    private static void m6_3(int x, int y) {
        for (int i = 1; i <= x; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.printf("%s * %s = %s\t", j, i, i * j);
            }
            System.out.println();
        }
    }

    private static void m7() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入文字：");
        String next = scanner.next();
        StringBuilder sb = new StringBuilder();
        for (int i = next.length() - 1; i >= 0; i--) {
            sb.append(next.charAt(i));
        }
        if (sb.toString().equals(next)) {
            System.out.println("输入的是回文！");
        } else {
            System.out.println("输入的不是回文！");
        }
    }

    private static void m8() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入质数：");
        int i = scanner.nextInt();
        for (int a = 2; a < i; a++) {
            if (i % a == 0) {
                System.out.println("输入的不是质数！");
                return;
            }
        }
        System.out.println("输入的是质数！");
    }

    private static void m9() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入华氏温度：");
        double i = scanner.nextDouble();
        i = (5.0 / 9.0) * (i - 32.0);
        System.out.printf("摄氏度为: %s°", i);
    }

    private static void fibonacci(int m) {
        int f1 = 1, f2 = 1, f3;
        System.out.println(f1);
        System.out.println(f2);
        while (true) {
            f3 = f1 + f2;
            if (f3 > m) break;
            System.out.println(f3);
            f1 = f2;
            f2 = f3;
        }
    }

    private static void m10() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入a：");
        double a = scanner.nextDouble();
        System.out.print("请输入b：");
        double b = scanner.nextDouble();
        System.out.print("请输入c：");
        double c = scanner.nextDouble();
        double s = (a + b + c) / 2;
        double num = Math.sqrt(s * (s - a) * (s - b) * (s - c));
        System.out.printf("边长%s,%s,%s的这个三角形的面积为: %s\n", a, b, c, num);
    }

    private static void m11() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入本年月份：");
        int month = scanner.nextInt();
        if (month > 12 && month < 0) {
            System.out.println("月份不合法!");
            return;
        }
        System.out.printf("%s年%s月的月历为:\n", LocalDate.now().getYear(), month);
        boolean leapYear = LocalDate.now().isLeapYear();
        int day;
        if (leapYear) {
            if (month == 2) {
                day = 29;
            } else if (month == 4 || month == 6 || month == 9 || month == 11) {
                day = 30;
            } else {
                day = 31;
            }
        } else {
            if (month == 2) {
                day = 28;
            } else if (month == 4 || month == 6 || month == 9 || month == 11) {
                day = 30;
            } else {
                day = 31;
            }
        }
        System.out.printf("日\t一\t二\t三\t四\t五\t六\n");
        for (int i = 1; i <= day; i++) {
            LocalDate of = LocalDate.of(LocalDate.now().getYear(), month, i);
            DayOfWeek dayOfWeek = of.getDayOfWeek();
            if (dayOfWeek.equals(SUNDAY)) {
                System.out.println();
            }
            if (i == 1) {
                int value = dayOfWeek.getValue();
                for (int j = 0; j < value; j++) {
                    System.out.print("\t");
                }
            }
            System.out.printf("%s\t", i);
        }
        System.out.println("\n-----------------------\n");
        System.out.print("请输入要获取星期几的日期: ");
        String date = scanner.next();
        DateTimeFormatter yyyyMMdd = DateTimeFormatter.ofPattern("yyyyMMdd");
        LocalDate parse = LocalDate.parse(date, yyyyMMdd);
        String xingqi = "";
        switch (parse.getDayOfWeek().getValue()) {
            case 1:
                xingqi = "星期一";
                break;
            case 2:
                xingqi = "星期二";
                break;
            case 3:
                xingqi = "星期三";
                break;
            case 4:
                xingqi = "星期四";
                break;
            case 5:
                xingqi = "星期五";
                break;
            case 6:
                xingqi = "星期六";
                break;
            default:
                xingqi = "星期日";
        }
        System.out.printf("%s 这天是%s\n", parse, xingqi);
        System.out.println("-----------------------\n");
        System.out.print("请输入计算天数差的第一个日期: ");
        String date1 = scanner.next();
        System.out.print("请输入计算天数差的第二个日期: ");
        String date2 = scanner.next();
        LocalDate parse1 = LocalDate.parse(date1, yyyyMMdd);
        LocalDate parse2 = LocalDate.parse(date2, yyyyMMdd);
        System.out.printf("%s 和 %s之间差 %s天", parse1, parse2, Math.abs(parse1.toEpochDay() - parse2.toEpochDay()));
    }

    private static void m12() {
        int yuan1 = 0, yuan5 = 0, yuan10 = 0, yuan20 = 0, yuan50 = 0, yuan100 = 0;

        int jiao1 = 0, jiao2 = 0, jiao5 = 0;

        int fen1 = 0, fen5 = 0;

        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入金额: ");
        double d = scanner.nextDouble();
        double num = d;
        while (d > 0) {
            if (d >= 100) {
                d -= 100;
                yuan100++;
            } else if (d >= 50 && d < 100) {
                d -= 50;
                yuan50++;
            } else if (d >= 20 && d < 50) {
                d -= 20;
                yuan20++;
            } else if (d >= 10 && d < 20) {
                d -= 10;
                yuan10++;
            } else if (d >= 5 && d < 10) {
                d -= 5;
                yuan5++;
            } else if (d >= 1 && d < 5) {
                d -= 1;
                yuan1++;
            }

            if (d >= 0.5 && d < 1) {
                d -= 0.5;
                jiao5++;
            } else if (d >= 0.2 && d < 0.5) {
                d -= 0.2;
                jiao2++;
            } else if (d >= 0.1 && d < 0.2) {
                d -= 0.1;
                jiao1++;
            }
            if (d >= 0.05 && d < 0.1) {
                d -= 0.05;
                fen5++;
            } else if (d >= 0.01 && d < 0.05) {
                d -= 0.01;
                fen1++;
            }
            d = Double.parseDouble(new DecimalFormat("0.##").format(d));
        }
        System.out.printf("%s 元=\n", num);
        System.out.printf("%s 个\t100元\n", yuan100);
        System.out.printf("%s 个\t50元\n", yuan50);
        System.out.printf("%s 个\t20元\n", yuan20);
        System.out.printf("%s 个\t10元\n", yuan10);
        System.out.printf("%s 个\t5元\n", yuan5);
        System.out.printf("%s 个\t1元\n", yuan1);
        System.out.printf("%s 个\t5角\n", jiao5);
        System.out.printf("%s 个\t2角\n", jiao2);
        System.out.printf("%s 个\t1角\n", jiao1);
        System.out.printf("%s 个\t5分\n", fen5);
        System.out.printf("%s 个\t1分\n", fen1);
    }

    private static void m13() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入整数:");
        int a = scanner.nextInt();
        StringBuilder sb = new StringBuilder(a + "");
        StringBuilder sb2 = new StringBuilder();
        for (int i = sb.length() - 1; i >= 0; i--) {
            sb2.append(sb.charAt(i));
        }
        int b = Integer.parseInt(sb2.toString());
        System.out.printf("转换后的结果为：%s\n", b);
    }

    private static void m14() {
        int[] arr = new int[100];
        for (int i = 0; i < 100; i++) {
            arr[i] = (int) (100 * Math.random());
            System.out.printf("%s\t",arr[i]);
        }
        int num = 0;
        Arrays.sort(arr);
        for (int i = 0; i < arr.length; i++) {
            if(arr[i] > 50){
                num++;
            }
        }
        System.out.println("\n最大值: " + arr[99]);
        System.out.println("最小值: " + arr[0]);
        System.out.println("大于50的次数: " + num);

    }
}
